package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_SearchKey;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

public class Act_Search extends HuobaoBaseActivity implements OnItemClickListener, View.OnClickListener {
    // 搜索框
    @ViewInject(R.id.edt_search)
    private EditText edt_search;
    // 商按钮
    @ViewInject(R.id.btn_1)
    private TextView btn_shang;
    // 店
    @ViewInject(R.id.btn_2)
    private TextView btn_dian;
    // grid
    @ViewInject(R.id.gridview)
    private GridView gridview;
    private Adapter_SearchKey adapter;

    @Override
    protected int getContentView() {
        return R.layout.layout_search;
    }

    @Override
    protected String getTitleText() {
        return null;
    }

    @Override
    protected void init() {
        btn_shang.setOnClickListener(this);
        btn_dian.setOnClickListener(this);
        gridview.setOnItemClickListener(this);
        mAbTitleBar.setVisibility(View.GONE);
        adapter = new Adapter_SearchKey(context);
        gridview.setAdapter(adapter);
        getData();
    }

    public void onBackClick(View view) {
        finish();
    }

    private void getData() {
        Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "search", HttpConst.SECOND_KEY,
                "hotkeywords");
        Http.getInstant().sendRequest(context, map, new HttpListener() {

            @Override
            public void onHttpSuccess(String result) {
                JSONObject jsonObject = JSON.parseObject(result);
                List<String> list = JSON.parseArray(jsonObject.getString("retval"), String.class);
                adapter.addAll(list);
            }

            @Override
            public void onHttpError(String error) {
                // TODO Auto-generated method stub

            }
        });
    }

    //店点击
    private void onDianClick() {
        Intent intent = new Intent(this, Act_StoreList.class);
        intent.putExtra("key", edt_search.getText().toString().trim());
        startActivity(intent);
    }

    //商点击
    private void onShangClick() {
        String key = edt_search.getText().toString().trim();
        if (TextUtils.isEmpty(key)) {
            EventBus.getDefault().post("1", "ChangeTag");
            finish();
        } else {
            SkipUtils.skipToSearchList(context, null, key, null, null, null, null);
            finish();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String str = adapter.getItem(position);
        SkipUtils.skipToSearchList(context, null, str, null, null, null, null);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_1:
                onShangClick();
                break;
            case R.id.btn_2:
                onDianClick();
                break;
        }
    }
}
